<?php
// +----------------------------------------------------------------------
// | FileName: AtlasController.php
// +----------------------------------------------------------------------
// | Date: 2023/3/24 16:35:27
// +----------------------------------------------------------------------
// | Author: 默毅 <moyi@mymoyi.cn>
// +----------------------------------------------------------------------
// | Notes: AI的图集 API
// +----------------------------------------------------------------------
	
	namespace App\Controller\Api;
	
	use App\Controller\AbstractController;
	use App\Model\Ai\Atlas\Atlas;
	use App\Model\Ai\Atlas\AtlasList;
	use App\Model\Ai\Drawing\DrawingTask;
	use App\Model\Ai\Picture;
	use App\Service\Library\CommonService;
	use App\Service\Library\DrawingService;
	use Hyperf\Di\Annotation\Inject;
	use Hyperf\HttpServer\Annotation\AutoController;
	use Hyperf\Redis\Redis;
	
	#[AutoController]
	class AtlasController extends AbstractController
	{
		#[Inject]
		private CommonService $CommonService;
		
		#[Inject]
		private DrawingService $DrawingService;
		
		#[Inject]
		private Redis $Redis;
		
		public function getList()
		{
			$pages = $this->request->post('pages',0);
			$limit = 10;
			$offset = $pages * $limit;
			
			$where = [
				['status', '=', 'normal'],
				['total', '>', '0'],
			];
			
			$list = Atlas::where($where)
				->with('picture:id,status,title,url')
				->with('tag_zh:ai_tag_zh.text,ai_tag_zh.label')
				->with('user:id,nickname,avatar')
				->offset($offset)
				->where($where)
				->limit($limit)
				->orderBy('id','desc')
				->get(['id', 'total', 'type', 'user_id', 'picture_id']);
			
			$more = Atlas::where($where)->count();
			
			return $this->success('', [
					'list' => $list,
					'pages'=>$pages+1,
					'more' => $offset+$limit < $more,
				]
			);
		}
		
		public function taskInfo(){
			$id = $this->request->post('id',1000);
			$where = [
				['status', '=', 'normal'],
				['id','=',$id]
			];
			print_r($where);
			$task_id = Picture::where($where)->value('task_id');
			print_r(['$task_id'=>$task_id]);
			if(!$task_id){
				return $this->error('当前图片无法制作同款');
			}else{
				$task = DrawingTask::find($task_id);
				$show_arr = [
					'height',
					'width',
					'model_hash',
					'vae_hash',
					'sampler_name',
					'steps',
					'type',
					'prompt',
					'negative_prompt',
					'zh_prompt',
					'zh_negative_prompt'
				];
				$info = [];
				foreach ($show_arr as $item){
					$info[$item] = $task[$item];
				}
				return $this->success('', $info);
			}
//			$pic = Picture::with('clip_tag_zh:ai_tag_zh.text,ai_tag_zh.label')
//				->with('deepbooru_tag_zh:ai_tag_zh.text,ai_tag_zh.label')
//				->find($list,['id','title','prompt','negative_prompt','url','model_name','model_hash','story']);
		}
		public function preview()
		{
			$id = $this->request->post('id',1000);
			$where = [
				['status', '=', 'normal'],
				['atlas_id','=',$id]
			];
			$list = AtlasList::where($where)
				->get(['picture_id as id']);
			$pic = Picture::with('clip_tag_zh:ai_tag_zh.text,ai_tag_zh.label')
				->with('deepbooru_tag_zh:ai_tag_zh.text,ai_tag_zh.label')
				->find($list,['id','title','prompt','negative_prompt','url','model_name','model_hash','story']);
			
			$where = [
				['status', '=', 'normal'],
				['total', '>', '0'],
			];
			
			return $this->success('错误测试',$pic->toArray());
		}
	}